using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    public static GameManager In;
    
    public GameState State;

    public GameObject BirdPrefab;
    public Material CaptureMaterial;
    private Transform BirdsRoot;
    public List<Bird> birds;
    
    public GameObject LanObj;
    private const float maxZ = 2;
    private const float maxX = 1;

    public float TimeScale = 1f;

    public AudioSource AudioSource;
    public AudioClip btnClip;
    public AudioClip closeClip;
    public AudioClip inClip;

    private void Awake()
    {
        In = this;
        LanObj.SetActive(false);
        BirdsRoot = new GameObject("BirdsRoot").transform;
        State = GameState.Begin;
    }

    void Update()
    {
        Time.timeScale = TimeScale;
    }

    public void OnBtnReset()
    {
        State = GameState.Begin;
        for (var i = birds.Count - 1; i >= 0; i--)
        {
            Destroy(birds[i].gameObject);
        }
        birds.Clear();
        GameData.In.Reset();
        UIManager.In.Refresh();
        AudioSource.PlayOneShot(closeClip);
    }

    public void OnBtnGameBegin()
    {
        State = GameState.Sign;
        var nums = RandomUtil.GetRandom(70, 120);
        GameData.In.BirdNums = nums;
        for (int i = 0; i < nums; i++)
        {
            GameObject bird = Instantiate(BirdPrefab, BirdsRoot);
            birds.Add(bird.GetComponent<Bird>());
        }
        UIManager.In.Refresh();
        AudioSource.PlayOneShot(btnClip);
    }

    public void OnValueChangeTime(float value)
    {
        TimeScale = value * 30 + 1;
    }

    public void OnBtnShowNum(bool show)
    {
        GameData.In.IsShowBirdNums = show;
        UIManager.In.Refresh();
        AudioSource.PlayOneShot(closeClip);
    }

    public void OnBtnSignBegin()
    {
        LanObj.SetActive(true);
        AudioSource.PlayOneShot(inClip);
    }
    
    public void OnBtnSignCancel()
    {
        LanObj.SetActive(false);
        AudioSource.PlayOneShot(inClip);
    }

    public void OnBtnSign()
    {
        State = GameState.Capture;
        int signNum = 0;
        for (var i = 0; i < birds.Count; i++)
        {
            Vector3 position = birds[i].transform.position;
            if (Mathf.Abs(position.x) < maxX && Mathf.Abs(position.z) < maxZ)
            {
                birds[i].Sign();
                ++signNum;
            }
        }
        LanObj.SetActive(false);
        GameData.In.SignBirdNums = signNum;
        UIManager.In.Refresh();
        AudioSource.PlayOneShot(btnClip);
    }
    
    public void OnBtnCaptureBegin()
    {
        LanObj.SetActive(true);
        AudioSource.PlayOneShot(inClip);
    }
    
    public void OnBtnCaptureCancel()
    {
        LanObj.SetActive(false);
        AudioSource.PlayOneShot(inClip);
    }

    public void OnBtnCapture()
    {
        int capNum = 0;
        int signNum = 0;
        for (var i = 0; i < birds.Count; i++)
        {
            Vector3 position = birds[i].transform.position;
            if (Mathf.Abs(position.x) < maxX && Mathf.Abs(position.z) < maxZ)
            {
                ++capNum;
                if (birds[i].IsSign)
                    ++signNum;
            }
        }
        LanObj.SetActive(false);
        GameData.In.LastCaptureBirdNums = capNum;
        GameData.In.LastCaptureSignBirdNums = signNum;
        GameData.In.AllCaptureBirdNums += capNum;
        GameData.In.AllCaptureSignBirdNums += signNum;
        State = GameState.AfterCapture;
        UIManager.In.Refresh();
        AudioSource.PlayOneShot(btnClip);
    }

    public void OnBtnCloseTip()
    {
        State = GameState.Capture;
        UIManager.In.Refresh();
        AudioSource.PlayOneShot(closeClip);
    }
}

public enum GameState
{
    Begin,
    Sign,
    Capture,
    AfterCapture,
}
